import dayjs from 'dayjs'

export function parseDate2Str(date: string | Date, format = 'YYYY-MM-DD') {
  if (!date) return ''
  return dayjs(date).format(format)
}

export function dateDiff(_hisTime: string, _nowTime: string) {
  if (!arguments.length) return ''

  const arg = arguments
  const now = arg[1] ? arg[1] : new Date().getTime()
  const diffValue = now - new Date(arg[0]).getTime()
  let result
  const minute = 1000 * 60
  const hour = minute * 60
  const day = hour * 24
  const month = day * 30
  const year = month * 12

  const _year = diffValue / year
  const _month = diffValue / month
  const _week = diffValue / (7 * day)
  const _day = diffValue / day
  const _hour = diffValue / hour
  const _min = diffValue / minute
  if (_year >= 1) result = parseInt(_year.toString()) + '年前'
  else if (_month >= 1) result = parseInt(_month.toString()) + '个月前'
  else if (_week >= 1) result = parseInt(_week.toString()) + '周前'
  else if (_day >= 1) result = parseInt(_day.toString()) + '天前'
  else if (_hour >= 1) result = parseInt(_hour.toString()) + '个小时前'
  else if (_min >= 1) result = parseInt(_min.toString()) + '分钟前'
  else result = '刚刚'
  return result
}

// 通过某一天获取某一周的开始与结束
export function getWeekByDay(value: number | string | Date | null) {
  const date = dayjs(value || undefined)
  const startDate = date.day(1)
  const endDate = date.day(7)
  return {
    start: startDate.format('YYYY-MM-DD'),
    end: endDate.format('YYYY-MM-DD'),
  }
}

export const dayMap = [
  {
    name: '一',
    id: 1,
  },
  {
    name: '二',
    id: 2,
  },
  {
    name: '三',
    id: 3,
  },
  {
    name: '四',
    id: 4,
  },
  {
    name: '五',
    id: 5,
  },
  {
    name: '六',
    id: 6,
  },
  {
    name: '天',
    id: 0,
  },
]
